﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class MediaProvider:DataAccess
    {
        public MediaProvider()
        {
            this.CacheDuration = Globals.Settings.Media.CacheDuration;
            this.EnableCaching = Globals.Settings.Media.EnableCaching;
            this.ConnectionString = Globals.Settings.ConnectionString;
        }

        #region IDataReader --> Object Picture
        protected virtual PictureDetails GetPictureFromReader(IDataReader reader)
        {
            return new PictureDetails(
                (int)reader["Id"],
                (byte[])reader["PictureBinary"],
                (string)reader["MimeType"],
                (string)reader["SeoFilename"],
                (bool)reader["IsNew"]);
        }

        protected virtual List<PictureDetails> GetPictureCollectionFromReader(IDataReader reader)
        {
            List<PictureDetails> picture = new List<PictureDetails>();
            while (reader.Read())
                picture.Add(GetPictureFromReader(reader));
            return picture;
        }

        #endregion

        #region Picture Method

        //public List<PictureDetails> ListPicture()
        //{
        //    using (SqlConnection cn = new SqlConnection(this.ConnectionString))
        //    {
        //        SqlCommand cmd = new SqlCommand("PictureList", cn);
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cn.Open();
        //        return GetPictureCollectionFromReader(ExecuteReader(cmd));
        //    }
        //}

        public PictureDetails PictureGetById(int Id)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("PictureGet", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
                cn.Open();
                IDataReader reader = ExecuteReader(cmd, CommandBehavior.SingleRow);
                if (reader.Read())
                    return GetPictureFromReader(reader);
                else
                    return null;
            }
        }

        public bool PictureDelete(int Id)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("PictureDelete", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
                cn.Open();
                int ret = ExecuteNonQuery(cmd);
                return (ret == 1);
            }
        }

        public List<PictureDetails> PictureList(int pageSize, int pageIndex)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("PictureList");
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
                cmd.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex;
                cn.Open();
                return GetPictureCollectionFromReader(ExecuteReader(cmd));
            }

        }

        public List<PictureDetails> ProductListByProductId(int productId)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("PictureGetByProductId");
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@ProductId", SqlDbType.Int).Value = productId;
                cn.Open();
                return GetPictureCollectionFromReader(ExecuteReader(cmd));
            }

        }

        public int PictureInsert(PictureDetails picture)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("PictureInsert", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@PictureBinary", SqlDbType.VarBinary).Value = picture.PictureBinary;
                cmd.Parameters.Add("@MimeType", SqlDbType.NVarChar).Value = picture.MimeType;
                cmd.Parameters.Add("@SeoFilename", SqlDbType.NVarChar).Value = picture.SeoFilename;
                cmd.Parameters.Add("@IsNew", SqlDbType.Bit).Value = picture.IsNew;
                cmd.Parameters.Add("@Id", SqlDbType.Int).Direction = ParameterDirection.Output;
                cn.Open();
                int ret = ExecuteNonQuery(cmd);
                return (int)cmd.Parameters["@Id"].Value;
            }
        }

        public bool PictureUpdate(PictureDetails picture)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("PictureUpdate", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@PictureBinary", SqlDbType.VarBinary).Value = picture.PictureBinary;
                cmd.Parameters.Add("@MimeType", SqlDbType.NVarChar).Value = picture.MimeType;
                cmd.Parameters.Add("@SeoFilename", SqlDbType.NVarChar).Value = picture.SeoFilename;
                cmd.Parameters.Add("@IsNew", SqlDbType.Bit).Value = picture.IsNew;
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = picture.Id;
                cn.Open();
                int ret = ExecuteNonQuery(cmd);
                return (ret == 1);
            }
        }

        #endregion
    }
}
